package com.lyy.weather.TIme;

import android.util.Log;

import com.lyy.weather.database.DatabaseUtil;
import com.lyy.weather.database.RefreshUtil;
import com.lyy.weather.network.HttpUtil;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Timerr {
    private static boolean flag = false;
    private TimeUtil timeUtil;
    private DatabaseUtil dbUtil;
    private RefreshUtil refreshUtil;
    public Timerr(TimeUtil timeUtil, DatabaseUtil dbUtil,RefreshUtil refreshUtil) {
        this.timeUtil = timeUtil;
        this.dbUtil = dbUtil;
       this.refreshUtil=refreshUtil;
    }

    public  void timeTask() {
        if (!flag) {
            // 初始化定时器--3秒钟
            final Timer timer = new Timer();

            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    try {

                        Log.d("TAG", "run:.... ");
                        tryToRefreshAll();
                        tryToRefresh24H();

//                      timer.cancel(); //关闭定时器线程
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, 1000, 10 * 1000); //delay: 从定时器初始化成功 开始启动的延迟时间。period：定时器的间隔时间
            flag = true;
        }
    }

    //尝试能否更新所有数据
    public void tryToRefreshAll()  {

        String latestUpdate=dbUtil.searchLatestTime();

        if(latestUpdate==null){//没有上一次的更新时间，直接更新
           // Log.d("TAG", "tryToRefreshAll.... ");
            refreshUtil.refreshAll();
            Log.d("TAG", "refreshing all of data succeed");
            return;
        }

        Calendar latestUpdateTime=null;
        try{
            latestUpdateTime=timeUtil.dateToCalendar(TimeUtil.getDateInFormat(latestUpdate));
        }catch(Exception e){
            e.printStackTrace();
        }

        Calendar currentTime=timeUtil.dateToCalendar(TimeUtil.getDate());

        if(TimeUtil.shouldItRefreshAll(latestUpdateTime,currentTime)){
            refreshUtil.refreshAll();
           Log.d("TAG", "refreshing all of data succeed");
        }
        Log.d("TAG", "Not refreshing all of data ");
    }
    //尝试能否更新所有的数据
    public void tryToRefresh24H()  {
        Log.d("TAG", "tryToRefresh24H.... ");
        List<String> locationIdList=refreshUtil.getLocationIdList();
        Iterator<String> iterator=locationIdList.iterator();

        while(iterator.hasNext()){

            String locationId=iterator.next();
            String time24H=dbUtil.searchLatest24hTime(locationId);
            Calendar latestUpdateTime=null;
            try{
                latestUpdateTime=timeUtil.dateToCalendar(TimeUtil.getDateInFormat(time24H));
            }catch (Exception e){
                e.printStackTrace();
            }


            if(TimeUtil.shouldItRefresh24H(latestUpdateTime,timeUtil.dateToCalendar(TimeUtil.getDate()))){
                refreshUtil.refresh24hWeather();
                Log.d("TAG", "refreshing 24h data succeed");
                break;
            }
        }
        Log.d("TAG", "Not refreshing 24h data");
    }

}
